package com.lk.ii.controller;

import java.util.List;

import com.lk.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.lk.common.annotation.Log;
import com.lk.common.enums.BusinessType;
import com.lk.ii.domain.IiEncyclopedia;
import com.lk.ii.service.IIiEncyclopediaService;
import com.lk.common.core.controller.BaseController;
import com.lk.common.core.domain.AjaxResult;
import com.lk.common.utils.poi.ExcelUtil;
import com.lk.common.core.page.TableDataInfo;

/**
 * 百科Controller
 * 
 * @author lk
 * @date 2021-02-22
 */
@Controller
@RequestMapping("/ii/encyclopedia")
public class IiEncyclopediaController extends BaseController
{
    private String prefix = "ii/encyclopedia";

    @Autowired
    private IIiEncyclopediaService iiEncyclopediaService;

    @RequiresPermissions("ii:encyclopedia:view")
    @GetMapping()
    public String encyclopedia()
    {
        return prefix + "/encyclopedia";
    }

    /**
     * 查询百科列表
     */
    @RequiresPermissions("ii:encyclopedia:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IiEncyclopedia iiEncyclopedia)
    {
        startPage();
        List<IiEncyclopedia> list = iiEncyclopediaService.selectIiEncyclopediaList(iiEncyclopedia);
        return getDataTable(list);
    }

    /**
     * 导出百科列表
     */
    @RequiresPermissions("ii:encyclopedia:export")
    @Log(title = "百科", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IiEncyclopedia iiEncyclopedia)
    {
        List<IiEncyclopedia> list = iiEncyclopediaService.selectIiEncyclopediaList(iiEncyclopedia);
        ExcelUtil<IiEncyclopedia> util = new ExcelUtil<IiEncyclopedia>(IiEncyclopedia.class);
        return util.exportExcel(list, "encyclopedia");
    }

    /**
     * 新增百科
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存百科
     */
    @RequiresPermissions("ii:encyclopedia:add")
    @Log(title = "百科", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IiEncyclopedia iiEncyclopedia)
    {
        iiEncyclopedia.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(iiEncyclopediaService.insertIiEncyclopedia(iiEncyclopedia));
    }

    /**
     * 修改百科
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        IiEncyclopedia iiEncyclopedia = iiEncyclopediaService.selectIiEncyclopediaById(id);
        mmap.put("iiEncyclopedia", iiEncyclopedia);
        return prefix + "/edit";
    }

    /**
     * 修改保存百科
     */
    @RequiresPermissions("ii:encyclopedia:edit")
    @Log(title = "百科", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IiEncyclopedia iiEncyclopedia)
    {
        iiEncyclopedia.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(iiEncyclopediaService.updateIiEncyclopedia(iiEncyclopedia));
    }

    /**
     * 删除百科
     */
    @RequiresPermissions("ii:encyclopedia:remove")
    @Log(title = "百科", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(iiEncyclopediaService.deleteIiEncyclopediaByIds(ids));
    }


}
